//
//  VisitorLoginView.swift
//  sinaWeibo
//
//  Created by LSY on 15/12/14.
//  Copyright © 2015年 lsy. All rights reserved.
//

import UIKit

//定义协议
@objc protocol VisitorLoginViewDelegate : NSObjectProtocol {
    
    //        声明协议方法,默认为必选方法
 optional   func userWillLogin()
            func userWillRegister()
    
}


class VisitorLoginView: UIView {

   
//    声明代理
   weak var visitorViewDelegate : VisitorLoginViewDelegate?
    
//    对外提供访客视图的方法
    func setupInfo(tipText:String?,imageName:String? ){
    
        tipLabel.text = tipText
        if let name = imageName {
            
             circleView.image = UIImage(named: name)
            largeIcon.hidden = true
        
//            将circleView作为顶层视图
            bringSubviewToFront(circleView)
        }else{
            
//            首页
//            首页开始动画
            startAnimation()
        }
    }
    
    
    
//    设置动画
  private func startAnimation(){
    
//        设置需要的动画类型
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
//        设置动画属性
    anim.repeatCount = MAXFLOAT
    anim.duration = 10.0
    anim.toValue = M_PI * 2
//    不移除图层
    anim.removedOnCompletion = false
//    添加动画
    
    
    circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
//    UIView默认的初始化方法
    override init(frame:CGRect){
    
        super.init(frame:frame)
//        设置界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        致命的错误
        fatalError("init(coder:) has not been implemented")
    }
    
//  在此view上  添加所有的子视图
    func setupUI(){
    
        addSubview(largeIcon)
        addSubview(circleView)
        addSubview(backView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
    
        
        //使用自动布局
        //NSLayoutConstraint  VFL
        /**
        *  返回一个自动布局约束对象
        *
        *  @param item         需要添加约束的对象
        *  @param attribute   添加约束的对象的需要约束的属性description#>
        *  @param relatedBy 约束的关系  一般使用 equal 表示精准约束
        *  @param toItem    相对约束的对象 description#>
        *  @param attribute 相对约束的兑现的属性
        *  @param multiplier           乘积值  一般 1
        *  @param constant           约束值
        *
        *   view1.attr1 = view2.attr2 * multiplier + constant
        自动布局原则 : 先固定好一个视图  其他所有视图 都应该相对于一个已经固定好的视图添加约束
        */
        //约束需要添加到父视图
        //在使用手码添加约束 需要屏蔽 frame布局
        
//        添加约束
        for subView in subviews {
            subView.translatesAutoresizingMaskIntoConstraints = false
        }
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        //添加圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))

//        设置遮罩
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView": backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView": backView,"regBtn":registerBtn]))
        
        //设置视图的背景颜色
        //UIColor(white: <#T##CGFloat#>, alpha: <#T##CGFloat#>) 设置灰度颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加点击事件
        registerBtn.addTarget(self, action: "registorBtnClicke", forControlEvents: .TouchUpInside)
        
        loginBtn.addTarget(self, action: "loginBtnClicke", forControlEvents: .TouchUpInside)

        
    }
    
//设置背景
    private lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
//    创建一个大图
  private  lazy var largeIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
  private  lazy var circleView : UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
   private lazy var tipLabel : UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
        
//  创建注册button
    private lazy var registerBtn : UIButton = UIButton(title: "注册", backImageName:"common_button_white_disable" , fontSize: 15, color: themeColor)
    
//    创建登录的btn
    private lazy var loginBtn:UIButton = UIButton(title: "登录", backImageName: "common_button_white_disable", fontSize: 15, color: themeColor)
    
    
//    添加btn的点击事件，使用代理方法实现
 @objc private  func registorBtnClicke(){
    
        visitorViewDelegate?.userWillRegister()
    
    }
    
  @objc private  func loginBtnClicke(){
    
    visitorViewDelegate?.userWillLogin?()
    }
    
    
}
